$(function() {
	init();
	
	$('#identity').keyup(function(evt){
	  if($('#identity').val().length == 1) {
		  var vOri=$('#identity').val();
		  var vNew=vOri.substring(0, 1).toUpperCase();
		  $('#identity').val(vNew);
	  }
	});
	
	$('#addUserSubmit').click(function() {
		var isValidata = true;
		var emailRegxp = /[\w-]+@([\w-]+\.)+[\w-]+/;
//		var valiTelephone = /[0-9]{2}\-[0-9]{7}/;
		var valiTelephone = /[0-9]{9}/;
//		var valiPhone =  /[0][1-9]{3}\-[0-9]{6}/;
		var valiPhone = /[0][1-9]{9}/;
		$('#msg').html('');
		
		$.each($('.isValidata'), function(i, o) {
			var ipt = $(o);
			if(ipt.val() == '') {
				ipt.focus();
				$('#msg').html('有星號欄位不得為空白!!!');
				isValidata = false;
				return false;
			}
		});
		
		if(!isValidata) {
			return false;
		}
		console.log($('#addLoginId').val().length);
		if($('#addLoginId').val().length < 8) {
			$('#addLoginId').focus();
			$('#msg').html('帳號長度太短!!!');
			isValidata = false;
			return false;
		}
		if(!valiUserID($('#identity').val())) {
			$('#identity').focus();
			$('#msg').html('請輸入正確的身份證字號!!!');
			isValidata = false;
			return false;
		}
			
		if(!emailRegxp.test($('#email').val())) {
			$('#msg').html('Email格式錯誤!!!');
			isValidata = false;
			$('#email').focus();
			return false;
		}
		
		if(!valiPhone.test($('#phone').val())) {
			$('#msg').html('手機格式不正確!!!');
			isValidata = false;
			$('#phone').focus();
			return false;
		}
		if($('#telephone').val() != '') {
			if(!valiTelephone.test($('#telephone').val())) {
				$('#msg').html('市話格式不正確!!!');
				isValidata = false;
				$('#telephone').focus();
				return false;
			}
		}
		
		if($('#county').val() == '' || $('#district').val() == '') {
			$('#msg').html('請選擇縣市!!!');
			isValidata = false;
			$('#county').focus();
			return false;
		}
		if($('#addPassword').val().length < 8 || !/^[A-Za-z0-9_-]+$/.test($('#password').val())) {
			$('#msg').html('密碼太短或密碼格式不正確請勿輸入非英文或數字!!!');
			isValidata = false;
			$('#addPassword').focus();
			return false;
		}
		
		if(isValidata) {
			var form = $('#addUserForm');
			form.attr('action', 'addUser!addUser.action');
			form.submit();
		}
	});
});

init = function() {
	twzipcode = $('#twzipcode');
	twzipcode.twzipcode(); 
	$('<input type="text" name="param.address" id="address" class="twzipcode isValidata" style="width: 210px"/>')
	.appendTo(twzipcode);
	$('#zipcode').val($('#zipcodeH').val());
	$('#county').val($('#countyH').val());
	$('#county').change();
	$('#district').val($('#districtH').val());
	$('#address').val($('#addressH').val());
};

valiUserID = function(userID) {
	var engMap = {A:10, B:11, C:12, D:13, E:14, F:15, G:16, H:17, I:34, J:18, K:19, L:20, M:21, N:20, O:35, P:23, Q:24, R:25, S:26, T:27, U:28, V:29, W:32, X:30, Y:31, Z:33};
	var engint = engMap[userID.substring(0,1).toUpperCase()] + userID.substring(1);
	var intArr = [1,9,8,7,6,5,4,3,2,1];
	var sum = 0;
	$.each(engint, function(i, o) {
		if(intArr[i] != 'undefined' && intArr[i] != null) {
			sum += intArr[i] * o;
		}
	});
	return 10 - sum%10 == parseInt(userID.substring(9));
};